<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
if($_RQ['store_id']){
	$this->session('store_id',$store_id);
}
$store_id=$this->session('store_id');
if(!$store_id){
	$store_id=pdo_getcolumn('jy_meye_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'),'id');
	$this->session('store_id',$store_id);
}
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['banner']=$basic['image'];
if($_RQ['op']=='list'){
	$serve_list=pdo_getall('jy_meye_store_serve',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'),'','','display asc');
	foreach($serve_list as &$item){
		//已支付，已确认，已完成订单数
		$server=$this->pdo_get('serve',array('id'=>$item['serve_id']));
		$item=array_merge($server,$item);
		$num1=pdo_count('jy_meye_server_order',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'serve_id'=>$item['id'],'status'=>'1'));
		$num2=pdo_count('jy_meye_server_order',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'serve_id'=>$item['id'],'status'=>'2'));
		$num3=pdo_count('jy_meye_server_order',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'serve_id'=>$item['id'],'status'=>'4'));
		$item['num']=$num1+$num2+$num3;
	}
	$return['pcd']=array(
		'province' =>trim($_RQ['province']),
		'city' =>trim($_RQ['city']),
		'district' =>trim($_RQ['district']),
	);
	$return['post']=array();
	$return['serve_list']=$serve_list;
	$return['sharedata']=array(
			'title'=>'项目列表',
			'desc'=>$basic['share_desc']?:'上门服务，技师入驻，经络排毒，活血化瘀！',
			'link'=>custom_url('jy_meye/server//fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true),
			'imgUrl'=>ATTACHMENT_URL.$basic['share_image'],
	);
	exi($return);
}
if($_RQ['op']=='sever_detail'){
	if(!$_RQ['id']){
		exi('没有找到该项目！','error');
	}
	//查询项目具体信息
	$serve=pdo_get('jy_meye_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$package=$serve['package'];
	//查询本项目可服务员工(未锁定，状态为可服务)
	$store_id=$this->session('store_id');
	$worker_list=pdo_getall('jy_meye_store_worker',array('store_id'=>$store_id,'uniacid'=>$_SESSION['uniacid'],'islock'=>'0','status'=>'1'));
	foreach($worker_list as $k=>$item){
		$service=explode(";",$item['service']);
		if(!in_array($_RQ['id'],$service)){
			unset($worker_list[$k]);
		}
	}
	//查询自定义字段
	$fileds=$serve['fileds']?json_decode($serve['fileds']):array();
	//查询本项目的访问人数及添加访问人数
	pdo_update('jy_meye_serve',array('num +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	//已支付，已确认，已完成订单数
	$num1=pdo_count('jy_meye_server_order',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'serve_id'=>$_RQ['id'],'status'=>'1'));
	$num2=pdo_count('jy_meye_server_order',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'serve_id'=>$_RQ['id'],'status'=>'2'));
	$num3=pdo_count('jy_meye_server_order',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'serve_id'=>$_RQ['id'],'status'=>'4'));
	$serve['ordernum']=$num1+$num2+$num3;
	$serve_time=json_decode($serve['serve_time'],true);
	//日期 今天，未来七天
	$data=array();
	$weekarray=array("日","一","二","三","四","五","六"); //先定义一个数组
	$days=$basic['days']>0?$basic['days']:7;
	for($i=0;$i<$days;$i++){
		$data[]=array('riqi'=>date("m-d",strtotime("+".$i." day")),'xinq'=>"星期".$weekarray[date("w",strtotime(date("Y-m-d",strtotime("+".$i." day"))))],'isshow'=>($serve['advancedays']>0&&$i<$serve['advancedays'])?-1:1);
	}
	$weekid=date("w");
	$weekid=$weekid?:7;
	$iswork=strpos($serve['work_time'],';'.$weekid.';');	
	$this->session('serve_id',$_RQ['id']);
	$serve['hdps']=json_decode($serve['hdps'],true);
	//获取当前时间
	$time=time();
	foreach($serve_time as $k=>&$item){
		$num=pdo_count('jy_meye_server_order',array('timeid'=>$k,'uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'day'=>date("Y-m-d",time("day")),'serve_id'=>$_RQ['id']));	
		if($item['yuyue']>$num){
			$item['yuyue']=$item['yuyue']-$num;
		}else{
			$item['yuyue']=0;
		}
		//时间转为时间戳
		$item['iswork']=$iswork;
		$item['shijian']=strtotime(date("Y-m-d",time("day")).$item['times']['start']);
		if($item['shijian']<time() || !$iswork ){
			$item['show']=0;
		}
		else{
			$item['show']=1;
		}
	}
	$return['serve']=$serve;
	$return['package']=json_decode($package);
	$return['worker_list']=$worker_list;
	$return['province']=file_get_contents('core/resource/area.txt');
	$return['province']=unserialize($return['province']);
	$return['serve_time']=$serve_time;
	$return['data']=$data;
	$return['post']=array(
		'dayid'=>0,
		'store_id'=>$store_id,
		'packageid'=>0,
	);
	$return['post']['fileds']=$fileds;
	$return['sharedata']=array(
			'title'=>'项目-'.$serve['title'],
			'desc'=>$serve['remark']?:'上门服务，技师入驻，经络排毒，活血化瘀！',
			'link'=>custom_url('jy_meye/server/detail/id='.$serve['id'].'&fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true),
			'imgUrl'=>ATTACHMENT_URL.$serve['image'],
	);
	exi($return);
}
if($_RQ['op']=='serve_time'){	
	if(!isset($_RQ['id'])){
		exi('请选择项目','error');
	}
	if(!isset($_RQ['day'])){
		exi('请重新选择日期','error');
	}
	$data=array();
	$data=$this->days($_RQ['id'],$basic['days']);
	$return['data']=$data;
	$post=get_postdata();
	$work_time=pdo_getcolumn('jy_meye_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'work_time');
	$weekid=date("w",strtotime(date("Y-m-d",strtotime("+".$_RQ['day']." day"))));
	$weekid=$weekid?:7;
	$iswork=strpos($work_time,';'.$weekid.';');
	$serve_time=pdo_getcolumn('jy_meye_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'serve_time');
	$serve_time=json_decode($serve_time,true);
	//获取当前时间
	$time=time();
	if($serve_time){
	foreach($serve_time as $k=>&$item){
		//已支付，已确认，已完成订单数
		$num=pdo_count('jy_meye_server_order',array('timeid'=>$k,'uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'day'=>$data[$_RQ['day']],'serve_id'=>$_RQ['id'],'status'=>array(1,2,4)));
		if($item['yuyue']>$num){
			$item['yuyue']=$item['yuyue']-$num;
		}else{
			$item['yuyue']=0;
		}
		$item['iswork']=$iswork;
		$item['weekid']=$weekid;
		$item['time']=time();
		//时间转为时间戳
		$item['shijian']=strtotime($data[$_RQ['day']]['ymd'].$item['times']['start']);
		if($item['shijian']<time() || $iswork ===false){
			$item['show']=0;
		}
		else{
			$item['show']=1;
		}	
	}
	}
	$return['serve_time']=$serve_time;
	$return['success']=1;
	exi($return);
}
if($_RQ['op']=='check_buy'){
	$store_id=$_RQ['store_id']?:$store_id;
	if(!$_RQ['worker_id']){
		exi('请选择服务人员','error');
	}
	exi('','','jy_meye/book//store_id='.$store_id.'&serve_id='.$_RQ['serve_id'].'&worker_id='.$_RQ['worker_id'].'&packageid='.$_RQ['packageid'].'&timeid='.$_RQ['timeid'].'&dayid='.$_RQ['dayid']);
}
if($_RQ['op']=='package'){
	if(!isset($_RQ['id'])){
		exi('请重新选择项目','error');
	}
	//exi($_RQ['id'],'error');
	$package=pdo_getcolumn('jy_meye_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'package');
	$package=json_decode($package,true);
	//查询自定义字段
	$fileds=pdo_getcolumn('jy_meye_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),'fileds');
	foreach($fileds as &$item){
		$item['value']=explode(';',$item['value']);
	}
	$return['post']=array();
	$return['post']['fileds']=$fileds;
	$return['package']=$package;
	$return['success']=1;
	exi($return);
}
if($_RQ['op']=='get_workers'){
	if(!$_RQ['id']){
		exi('请选择项目！','error');
	}
	if(!$_RQ['store_id']){
		exi('请选择门店！','error');
	}
	$worker_list=pdo_getall('jy_meye_store_worker',array('store_id'=>$_RQ['store_id'],'uniacid'=>$_SESSION['uniacid'],'islock'=>'0','status'=>'1'));
	foreach($worker_list as $k=>$item){
		$service=explode(";",$item['service']);
		if(!in_array($_RQ['id'],$service)){
			unset($worker_list[$k]);
		}
	}
	$return['list']=$worker_list;
	exi($return);
}
if($_RQ['op']=='get_store'){
	if($_RQ['lat'] || $_RQ['lng']){
		$lat_lng=array('lat'=>$_RQ['lat'],'lng'=>$_RQ['lng']);
		$this->session('lat_lng',$lat_lng);
	}else{
		$lat_lng=$this->session('lat_lng');
	}
	//门店信息
	$store=pdo_get('jy_meye_store',array('id'=>$store_id));
	$store['images']=json_decode($store['images'],true);
	$store['images'][0]=$store['images'][0]?:'../core/resource/images/nopic.jpg';
	$store['image']=$store['images'][0];
	$store['time']=json_decode($store['time'],true);
	$store['service']=json_decode($store['service'],true);
	$store['lat_lng']=json_decode($store['lat_lng'],true);
	if($lat_lng){
		$store['distance']=cfc('tools')->getdistance($lat_lng['lng'],$lat_lng['lat'],$store['lat_lng']['lng'],$store['lat_lng']['lat']);
		$store['distance']=intval($v['distance']/1000);
	}
	$return['store']=$store;
	exi($return);
}